package com.qianfeng.controller;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Employee;
import com.qianfeng.enums.CommonEnum;
import com.qianfeng.exceptions.CommonException;
import com.qianfeng.service.EmployeeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getEmployeePage")
    public Result getEmployeePage(Integer pageNo, Integer pageSize) {
        Result result = new Result();
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        Page employeePage = employeeService.getEmployeePage(pageNo, pageSize);
//        System.out.println(employeePage);
        result.setObj(employeePage);
        result.setMessage(new Message(200, "成功"));
        return result;
    }

    /**
     * 添加
     *
     * @param employee
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public Result addEmployee(Employee employee) {
//        System.out.println("添加employee"+employee);
        try {
            //设置盐值
            String salt = String.valueOf(System.currentTimeMillis());
            //加密
            Md5Hash md5Hash = new Md5Hash(employee.getEmpPassword(), salt, 1024);
            String newPassword = md5Hash.toString();
            Employee employee1 = new Employee();
            employee1.setEmpName(employee.getEmpName());
            employee1.setDeptId(employee.getDeptId());
            employee1.setGender(employee.getGender());
            employee1.setPhone(employee.getPhone());
            employee1.setQq(employee.getQq());
            employee1.setEmail(employee.getEmail());
            employee1.setHireTime(employee.getHireTime());
            employee1.setEmpPassword(newPassword);
            employee1.setRoleId(employee.getRoleId());
            employee1.setSalt(salt);
            Result result = new Result();
            employeeService.addEmployee(employee1);
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.ADD_EMPLOYEE_ERROR);
        }
    }

    /**
     * 删除
     *
     * @param empId
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/deleteEmployee")
    public Result deleteEmployee(Integer empId) {
        try {
            employeeService.deleteEmployee(empId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.DELETE_EMPLOYEE_ERROR);
        }
    }

    /**
     * 更新
     *
     * @param employee
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/updateEmployee", method = RequestMethod.POST)
    public Result updateEmployee(Employee employee) {
//        System.out.println("更新"+employee);
        try {
            //设置盐值
            String salt = String.valueOf(System.currentTimeMillis());
            //加密
            Md5Hash md5Hash = new Md5Hash(employee.getEmpPassword(), salt, 1024);
            String newPassword = md5Hash.toString();
            Employee employee1 = new Employee();
            employee1.setEmpName(employee.getEmpName());
            employee1.setDeptId(employee.getDeptId());
            employee1.setGender(employee.getGender());
            employee1.setPhone(employee.getPhone());
            employee1.setQq(employee.getQq());
            employee1.setEmail(employee.getEmail());
            employee1.setHireTime(employee.getHireTime());
            employee1.setEmpPassword(newPassword);
            employee1.setRoleId(employee.getRoleId());
            employee1.setEmpId(employee.getEmpId());
            employee1.setSalt(salt);
            Result result = new Result();
            employeeService.updateEmployee(employee1);
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.UPDATE_EMPLOYEE_ERROR);
        }
    }

    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getEmployeeByEmpId")
    public Result getEmployeeByEmpId(Integer empId) {
        Result result = new Result();
        Employee employee = employeeService.getEmployee(empId);
        result.setMessage(new Message());
        result.setObj(employee);
        return result;
    }


}
